
'''
    生成训练数据集
'''
def process_train_feature():
    # days_df = pd.read_csv('input/origin_yanmu_v3_gn_hy.csv',index_col=0).reset_index()[[ 'date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','high_limit', 'turnover', 't_rate', 'xl', 'xl_rate', 'gn_zt_ratio','hy_zt_ratio']]
    days_df = pd.read_csv('input/origin_yanmu_v3_2017_2025.csv',index_col=0).reset_index()[[ 'date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','high_limit', 'turnover', 't_rate', 'xl', 'xl_rate']]
    days_df = days_df[days_df['date'] <= '2025-04-03']
    days_df = days_df.reset_index().round(2)
    days_df = days_df.drop(labels='index', axis=1)  # 返回新对象
    p_col_len(days_df)
    
    week_df = pd.read_csv('input/week_all_data.csv',index_col=0)
    week_df = week_df.reset_index().round(2)
    p_col_len(week_df)

    execfile('yanmu_v3/yanmu_high.py')
    execfile('yanmu_v3/yanmu_util.py')
    high_df = mark_high(days_df,week_df)
    p_col_len(high_df)

    execfile('yanmu_v3/yanmu_raise.py')
    raise_df = mark_raise(days_df)
    p_col_len(raise_df)       

    execfile('yanmu_v3/yanmu_ma.py')
    ma_df = mark_d_ma(days_df) 
    p_col_len(ma_df)
    ma_d_w_df = mark_w_ma(days_df,week_df)
    p_col_len(ma_d_w_df)

    execfile('yanmu_v3/yanmu_xl.py')
    xl_df = mark_xl(days_df)
    p_col_len(xl_df)
    execfile('yanmu_v3/yanmu_turnover.py')
    vol_turnover_df = mark_vol_turnover(days_df)  
    p_col_len(vol_turnover_df)

    execfile('yanmu_v3/yanmu_turnover.py')
    turnover_yoy_df = mark_col_growth(days_df, 'turnover', window=1,is_drop=True)
    p_col_len(turnover_yoy_df)

    execfile('yanmu_v3/yanmu_xl.py')
    xl_yoy_df = mark_xl_col_growth(days_df, 'xl', window=1,is_drop=True)
    p_col_len(xl_yoy_df)    
      

    execfile('yanmu_v3/yanmu_util.py')
    # final_days_df = days_df[['date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','turnover', 't_rate','xl', 'xl_rate','gn_zt_ratio','hy_zt_ratio' ]]
    final_days_df = days_df[['date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','turnover', 't_rate','xl', 'xl_rate' ]]

    final_days_df['float_mv'] = final_days_df['turnover']*100/final_days_df['t_rate']
    dfs = [final_days_df,high_df,raise_df,ma_df,ma_d_w_df, xl_df,vol_turnover_df,turnover_yoy_df, xl_yoy_df]
    all_feature_df = merge_all_df(dfs)
    p_col_len(all_feature_df)

    '''获取指数特征'''
    execfile('yanmu_v3/yanmu_zhishu.py')
    execfile('yanmu_v3/yanmu_raise.py')
    execfile('yanmu_v3/yanmu_ma.py')

    zhishu_df = pd.read_csv('input/predict_v2_zhishu.csv')
    zhishu_feature = get_zhishu_raise_ma_df(zhishu_df)
    p_col_len(zhishu_feature)

    f_l_df = merge_zhishu(all_feature_df,zhishu_feature)
    
    label_df  = mark_label(days_df)
    f_l_df = pd.merge(f_l_df,label_df,on=['date','code'])
    p_col_len(f_l_df)

    execfile('yanmu_v3/yanmu_vol_raise_score.py')
    f_l_df = mark_vol_raise_score(f_l_df)
    print(len(f_l_df))
    p_col_len(f_l_df)

    f_l_df.to_csv('output/v3_train_feature.csv',index=False,encoding='utf-8')
  




'''
    预测数据集特征
'''
def process_predict_feature(filename):
    # days_df = pd.read_csv('input/'+str(filename))[['date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','high_limit', 'turnover', 't_rate', 'xl', 'xl_rate','gn_zt_ratio','hy_zt_ratio']]
    days_df = pd.read_csv('input/'+str(filename))[['date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','high_limit', 'turnover', 't_rate', 'xl', 'xl_rate']]

    p_col_len(days_df)
    week_predict_df = pd.read_csv('input/predict_v3_week.csv',index_col=0)
    week_df = pd.read_csv('input/week_all_data.csv',index_col=0).reset_index().round(2)
    week_df = pd.concat([week_df,week_predict_df],ignore_index=True).drop_duplicates(subset=['date','code']).reset_index(drop=True)
    p_col_len(week_df)
    
    
    execfile('yanmu_v3/yanmu_high.py')
    execfile('yanmu_v3/yanmu_util.py')
    high_df = mark_high(days_df,week_df)
    p_col_len(high_df)

    execfile('yanmu_v3/yanmu_raise.py')
    raise_df = mark_raise(days_df)
    p_col_len(raise_df)       

    execfile('yanmu_v3/yanmu_ma.py')
    ma_df = mark_d_ma(days_df) 
    p_col_len(ma_df)
    ma_d_w_df = mark_w_ma(days_df,week_df)
    p_col_len(ma_d_w_df)

    execfile('yanmu_v3/yanmu_xl.py')
    xl_df = mark_xl(days_df)
    p_col_len(xl_df)
    execfile('yanmu_v3/yanmu_turnover.py')
    vol_turnover_df = mark_vol_turnover(days_df)  
    p_col_len(vol_turnover_df)

    execfile('yanmu_v3/yanmu_turnover.py')
    turnover_yoy_df = mark_col_growth(days_df, 'turnover', window=1,is_drop=True)
    p_col_len(turnover_yoy_df)

    execfile('yanmu_v3/yanmu_xl.py')
    xl_yoy_df = mark_xl_col_growth(days_df, 'xl', window=1,is_drop=True)
    p_col_len(xl_yoy_df)    

    execfile('yanmu_v3/yanmu_util.py')
    # final_days_df = days_df[['date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','turnover', 't_rate','xl', 'xl_rate','gn_zt_ratio','hy_zt_ratio']]
    final_days_df = days_df[['date', 'code', 'open', 'close', 'high', 'low', 'quote_rate','turnover', 't_rate','xl', 'xl_rate',]]

    final_days_df['float_mv'] = final_days_df['turnover']*100/final_days_df['t_rate']
    dfs = [final_days_df,high_df,raise_df,ma_df,ma_d_w_df, xl_df,vol_turnover_df,turnover_yoy_df, xl_yoy_df]
    all_feature_df = merge_all_df(dfs)
    p_col_len(all_feature_df)

    '''获取指数特征'''
    execfile('yanmu_v3/yanmu_zhishu.py')
    execfile('yanmu_v3/yanmu_raise.py')
    execfile('yanmu_v3/yanmu_ma.py')

    zhishu_df = pd.read_csv('input/predict_v3_zhishu.csv')
    zhishu_feature = get_zhishu_raise_ma_df(zhishu_df)
    p_col_len(zhishu_feature)

    f_l_df = merge_zhishu(all_feature_df,zhishu_feature)

    execfile('yanmu_v3/yanmu_vol_raise_score.py')
    f_l_df = mark_vol_raise_score(f_l_df)
    print(len(f_l_df))
    p_col_len(f_l_df)

    f_l_df.to_csv('output/v3_predict_feature.csv',index=False,encoding='utf-8')
    print('save v3_predict_feature.csv success')



def merge_zhishu(feature_df,zhishu_feature_df):
    feature_df['date'] = pd.to_datetime(feature_df['date'])
    zhishu_feature_df['date'] = pd.to_datetime(zhishu_feature_df['date'])
    merged_df = pd.merge(
        feature_df,
        zhishu_feature_df,
        on='date',          # 关联键
        how='left',          # 左连接保留所有df1数据
        validate='m:1'       # 验证df2的date唯一性（确保无重复）
    )
    return merged_df


""" 标注后续表现（计算未来N日收益率）"""
def label_future(df, days):
    col = 'ret'+str(days)
    df['future'] = df.groupby('code')['close'].shift(-days)
    df[col] = (df['future'] / df['close'] - 1)
    # df = df[['date','code',col]]
    return df

def mark_label(df):
    df = df.sort_values(['code', 'date'])
    #计算多个时间窗口的收益率
    label_array = [1,2,3,5]
    for days in label_array:
        df = label_future(df, days)
    # 只保留需要的列
    keep_cols = ['date', 'code'] + [f'ret{d}' for d in label_array]
    df = df[keep_cols]

    df['date'] = pd.to_datetime(df['date'])
    df = df.round(2)
    return df


def p_col_len(df):
    print(len(df),'\n  ',df.columns)



''' 计算周涨幅 '''
def calculate_weekly_raise2(df):
    # 预处理：确保按日期排序
    df = df.sort_values(['code', 'date']).copy()
    df['date'] = pd.to_datetime(df['date'])
    
    def _cal_week(g):
        # 生成精准周标记（ISO周）
        g['week'] = g['date'].dt.isocalendar().year.astype(str) + '-' + \
                   g['date'].dt.isocalendar().week.astype(str).str.zfill(2)
        
        # 周内按日期排序后累计
        g = g.sort_values('date')
        g['w_raise'] = g.groupby('week')['quote_rate'].cumsum()
        return g.drop(columns='week')
    
    return df.groupby('code', group_keys=False).apply(_cal_week)


